#include "connectdb.h"
#include "ui_connectdb.h"
#include "qevent.h"
#include "flatui.h"
#include <QMessageBox>
#include <QFile>
#include "db.h"
#include "tool.h"
#include <QDebug>
#include "mainscene.h"
#include <QSound>
#include <QTimer>

ConnectDB::ConnectDB(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ConnectDB)
{
    ui->setupUi(this);
    this->initForm();
}

ConnectDB::~ConnectDB()
{
    delete ui;
}

void ConnectDB::initForm()
{
    //设置窗口标题
    this->setWindowTitle("ShopSystem");
    //限定IP范围和端口范围
    this->ui->lineEdit_ip1->setValidator(new QIntValidator(0, 999, this));
    this->ui->lineEdit_ip2->setValidator(new QIntValidator(0, 999, this));
    this->ui->lineEdit_ip3->setValidator(new QIntValidator(0, 999, this));
    this->ui->lineEdit_ip4->setValidator(new QIntValidator(0, 999, this));
    this->ui->lineEdit_port->setValidator(new QIntValidator(0, 65536, this));
    //ui美化
    FlatUI::setPushButtonQss(ui->pushButton_Help);
    FlatUI::setPushButtonQss(ui->pushButton_TestConnect, 5, 8, "#1ABC9C", "#E6F8F5", "#2EE1C1", "#FFFFFF", "#16A086", "#A7EEE6");
    FlatUI::setPushButtonQss(ui->pushButton_SaveConfig, 5, 8, "#3498DB", "#FFFFFF", "#5DACE4", "#E5FEFF", "#2483C7", "#A0DAFB");
    FlatUI::setPushButtonQss(ui->pushButton_Entry, 5, 8, "#E74C3C", "#FFFFFF", "#EC7064", "#FFF5E7", "#DC2D1A", "#F5A996");
    FlatUI::setLineEditQss(ui->lineEdit_ip1, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_ip2, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_ip3, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_ip4, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_name, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_port, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_user, 5, 2, "#DCE4EC", "#1ABC9C");
    FlatUI::setLineEditQss(ui->lineEdit_password, 5, 2, "#DCE4EC", "#1ABC9C");
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
    ui->widget_picture->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(4));
    //设置按钮气泡
    ui->pushButton_Help->setToolTip("help");
    ui->pushButton_Close->setToolTip("close");
    ui->pushButton_Entry->setToolTip("enter");
    ui->pushButton_SaveConfig->setToolTip("save config");
    ui->pushButton_TestConnect->setToolTip("test connect");
    //初始化lineEdit内容
    QString ip = Tool::fileRead("config/ipconfig.ini");
    if (ip != ""){
        QStringList ipifm = ip.split(".");
        ui->lineEdit_ip1->setText(ipifm[0]);
        ui->lineEdit_ip2->setText(ipifm[1]);
        ui->lineEdit_ip3->setText(ipifm[2]);
        ui->lineEdit_ip4->setText(ipifm[3]);
    }
    ui->lineEdit_port->setText(Tool::fileRead("config/portconfig.ini"));
    ui->lineEdit_name->setText(Tool::fileRead("config/nameconfig.ini"));
    ui->lineEdit_user->setText(Tool::fileRead("config/userconfig.ini"));
    ui->lineEdit_password->setText(Tool::toUpperAndLower(QByteArray::fromBase64(Tool::fileRead("config/pwdconfig.ini").toLocal8Bit())));
}

void ConnectDB::saveconfig(){
    QFile file("config/ipconfig.ini");
    file.remove();
    if (file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QTextStream stream(&file);
        QString qs = "";
        qs += ui->lineEdit_ip1->text() + ".";
        qs += ui->lineEdit_ip2->text() + ".";
        qs += ui->lineEdit_ip3->text() + ".";
        qs += ui->lineEdit_ip4->text();
        stream << qs;
        file.close();
    }
    QFile file2("config/nameconfig.ini");
    file2.remove();
    if (file2.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QTextStream stream(&file2);
        stream << ui->lineEdit_name->text();
        file2.close();
    }
    QFile file3("config/portconfig.ini");
    file3.remove();
    if (file3.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QTextStream stream(&file3);
        stream << ui->lineEdit_port->text();
        file3.close();
    }
    QFile file4("config/userconfig.ini");
    file4.remove();
    if (file4.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QTextStream stream(&file4);
        stream << ui->lineEdit_user->text();
        file4.close();
    }
    QFile file5("config/pwdconfig.ini");
    file5.remove();
    if (file5.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QTextStream stream(&file5);
        stream << Tool::toUpperAndLower(ui->lineEdit_password->text()).toLocal8Bit().toBase64();
        file5.close();
    }
}

void ConnectDB::on_pushButton_Close_clicked()
{
    //按钮音效
    QSound *closeSound = new QSound(":/sound/BackButtonSound.wav",this);
    closeSound->play();
    QTimer::singleShot(500, this,[=](){
         QApplication::quit();
     }
    );
}

void ConnectDB::on_pushButton_Help_clicked()
{
    //按钮音效
    QSound *closeSound = new QSound(":/sound/TapButtonSound.wav",this);
    closeSound->play();
    QTimer::singleShot(300, this,[=](){
        QString dlgTitle="注意";
        QString strInfo="请使用已配置的数据库进行连接\n使用配置工具对未配置的数据库\n进行配置";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
     }
    );

}

void ConnectDB::on_pushButton_SaveConfig_clicked()
{
    //按钮音效
    QSound *closeSound = new QSound(":/sound/TapButtonSound.wav",this);
    closeSound->play();
    QTimer::singleShot(300, this,[=](){
        this->saveconfig();
        QString dlgTitle="successful";
        QString strInfo="保存成功";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
    );
}

void ConnectDB::on_pushButton_TestConnect_clicked()
{
    //按钮音效
    QSound *closeSound = new QSound(":/sound/TapButtonSound.wav",this);
    closeSound->play();
    QTimer::singleShot(300, this,[=](){
        QString ip = ui->lineEdit_ip1->text() + "." + ui->lineEdit_ip2->text() + "." + ui->lineEdit_ip3->text() + "." + ui->lineEdit_ip4->text();
        QString port = ui->lineEdit_port->text();
        QString name = ui->lineEdit_name->text();
        QString user = ui->lineEdit_user->text();
        QString password = ui->lineEdit_password->text();
        if( Db::testDb(ip,port,name,user,password)){
            QString dlgTitle="test";
            QString strInfo="连接成功";
            QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        }else{
            QString dlgTitle="test";
            QString strInfo="连接失败";
            QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
        }
    });
}

void ConnectDB::on_pushButton_Entry_clicked()
{
    //按钮音效
    QSound *closeSound = new QSound(":/sound/TapButtonSound.wav",this);
    closeSound->play();
    QTimer::singleShot(300, this,[=](){
    QString ip = ui->lineEdit_ip1->text() + "." + ui->lineEdit_ip2->text() + "." + ui->lineEdit_ip3->text() + "." + ui->lineEdit_ip4->text();
    QString port = ui->lineEdit_port->text();
    QString name = ui->lineEdit_name->text();
    QString user = ui->lineEdit_user->text();
    QString password = ui->lineEdit_password->text();
    if( Db::testDb(ip,port,name,user,password)){
        this->close();
        this->saveconfig();
        MainScene *m = new MainScene();
        m->show();
    }else{
        QString dlgTitle="fail";
        QString strInfo="连接"+ip+":"+port+"失败";
        QMessageBox::information(this, dlgTitle, strInfo,QMessageBox::Ok,QMessageBox::NoButton);
    }
    });
}
